const express = require('express');
const timeout = require('connect-timeout');
const { createProxyMiddleware } = require("http-proxy-middleware");
const app = express();
 

// 超时时间
const TIME_OUT = 30 * 1e3;
 
// 设置端口
app.set('port', '7527');
 
// 设置超时 返回超时响应
app.use(timeout(TIME_OUT));

app.use((req, res, next) => {
 if (!req.timedout) next();
});
 
 
proxyOption = {
//  target: 'http://120.55.58.14:60080',
//  target: 'http://192.168.0.51:7001',
 target: 'http://www.nbjggc.com',
 pathRewrite: {
    '^/apiV2/' : '/' // 重写请求，api/解析为/
  },
  changeOrigoin:true
}
 
// // 静态资源路径
// app.use('/', express.static('src/page'));
 
app.all('*', (req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",' 3.2.1')
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});

// 反向代理
app.use('/apiV2/*', createProxyMiddleware(proxyOption));
 
// 监听端口
app.listen(app.get('port'), () => {
 console.log(`server running @${app.get('port')}`);
})